package com.xxxx.server.service.impl;

//import com.alibaba.excel.EasyExcel;
//import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
//import com.xxxx.server.listener.UploadDataListener;
import com.xxxx.server.mapper.EmployeeEcMapper;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.EmployeeEc;
import com.xxxx.server.query.EmployeeEcQuery;
import com.xxxx.server.service.IEmployeeEcService;
import com.xxxx.server.utils.AssertUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Service
public class EmployeeEcServiceImpl extends ServiceImpl<EmployeeEcMapper, EmployeeEc> implements IEmployeeEcService {

    @Resource
    private EmployeeEcMapper employeeEcMapper;
    @Resource
    private EmployeeMapper employeeMapper;
//    @Resource
//    private UploadDataListener uploadDataListener;

    /**
     * 员工奖惩列表
     * @param query
     * @return
     */
    @Override
    public Map<String, Object> select(EmployeeEcQuery query) {
        if (query.getName()==""){
            query.setName(null);
        }
        if (query.getFromDate()==null&&query.getToDate()==null){
            query.setFromDate(null);
            query.setToDate(null);
        }
        Map<String, Object> map = new HashMap<>();
        List<EmployeeEc> employeeEcs = employeeEcMapper.queryList(query);
        PageHelper.startPage(query.getCurrentPage(), query.getSize());
        PageInfo<EmployeeEc> pageInfo = new PageInfo<>(employeeEcMapper.queryList(query));
        map.put("code", 0);
        map.put("msg", "success");
        map.put("total", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 员工奖惩添加
     * @param employeeEc
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void add(EmployeeEc employeeEc) {
        check(employeeEc);
        AssertUtil.isTrue(employeeEcMapper.insert(employeeEc)!=1, "员工奖惩添加失败！");
    }

    /**
     * 员工奖惩修改
     * @param employeeEc
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void update(EmployeeEc employeeEc) {
        AssertUtil.isTrue(null==employeeEc.getId(), "员工奖惩id为空!");
        AssertUtil.isTrue(null==employeeEcMapper.selectById(employeeEc.getId()), "员工奖惩id为空!");
        check(employeeEc);
        AssertUtil.isTrue(employeeEcMapper.updateById(employeeEc)!=1, "员工奖惩修改失败！");
    }

    /**
     * 员工奖惩删除
     * @param id
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public void delete(Integer id) {
        AssertUtil.isTrue(null==id, "员工奖惩id为空！");
        AssertUtil.isTrue(employeeEcMapper.deleteById(id)!=1, "员工奖惩删除失败！");
    }

    @Override
    public void export(EmployeeEcQuery query, HttpServletRequest request, HttpServletResponse response) {

    }

    @Override
    public void upload(MultipartFile file) {

    }

//    /**
//     * 导出文件
//     * @param request
//     * @param response
//     */
//    @Override
//    public void export(EmployeeEcQuery query,HttpServletRequest request, HttpServletResponse response){
//        // 文件后缀以xls
////        response.setContentType("application/vnd.ms-excel");
//        // 文件后缀以xlsx
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
//        response.setCharacterEncoding("utf-8");
//        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
//        try {
//            String fileName = URLEncoder.encode("员工奖惩", "UTF-8");
//            response.setHeader("Content-disposition", "attachment;filename=" + fileName +".xlsx");
//            EasyExcel.write(response.getOutputStream(),EmployeeEc.class).excelType(ExcelTypeEnum.XLSX).sheet(0, "模板").doWrite(data(query));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

//    @Override
//    public void upload(MultipartFile file) {
//        try {
//            EasyExcel.read(file.getInputStream(), EmployeeEc.class, uploadDataListener).sheet(0, "模板").doRead();
//        } catch (IOException e) {
//            e.printStackTrace();
//            System.out.println("上传异常！");
//        }
//    }

    private List data(EmployeeEcQuery query) {
        query.setCurrentPage(1);
        query.setSize(10000);
        List<EmployeeEc> employeeEcs = employeeEcMapper.queryList(query);
        return employeeEcs;
    }

    /**
     * 校验数据
     * @param employeeEc
     */
    private void check(EmployeeEc employeeEc) {
        AssertUtil.isTrue(null == employeeEc.getEid(), "员工ID不存在！");
        Employee employee = employeeMapper.selectById(employeeEc.getEid());
        AssertUtil.isTrue(null == employee, "员工ID不存在！");
        System.out.println(employeeEc.toString());
        AssertUtil.isTrue(null==employeeEc.getName()||"".equals(employeeEc.getName()), "员工姓名不存在！");
        AssertUtil.isTrue(!employeeEc.getName().equals(employee.getName()), "员工姓名与数据中姓名不相同！");
        AssertUtil.isTrue(null == employeeEc.getEcDate()||"".equals(employeeEc.getEcDate()), "奖惩时间为空！");
        AssertUtil.isTrue(null == employeeEc.getEcReason()||"".equals(employeeEc.getEcReason()), "奖惩原因为空！");
        AssertUtil.isTrue(null == employeeEc.getEcPoint()||"".equals(employeeEc.getEcPoint()), "奖惩分数为空！");
        AssertUtil.isTrue(null == employeeEc.getEcType()||"".equals(employeeEc.getEcType()), "奖惩类别为空！");
    }
}
